package com.wyh.rabbitextensionsdemo.config;

import com.wyh.rabbitextensionsdemo.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
    @Bean("ackQueue")
    public Queue ackQueue(){
        return QueueBuilder.durable(Constants.ACK_QUEUE).build();
    }

    @Bean("ackExchange")
    public DirectExchange ackExchange(){
        return ExchangeBuilder.directExchange(Constants.ACK_EXCHANGE).build();
    }

    @Bean("ackQueueBinding")
    public Binding ackQueueBinding(@Qualifier("ackExchange") DirectExchange exchange,
                                   @Qualifier("ackQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(Constants.ACK_ROUTING_KEY);
    }

    @Bean("preQueue")
    public Queue preQueue(){
        return QueueBuilder.nonDurable(Constants.PRE_QUEUE).build();
    }

    @Bean("preExchange")
    public DirectExchange preExchange(){
        return ExchangeBuilder.directExchange(Constants.PRE_EXCHANGE).durable(false).build();
    }

    @Bean("preQueueBinding")
    public Binding preQueueBinding(@Qualifier("preExchange") DirectExchange exchange,
                                   @Qualifier("preQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(Constants.PRE_ROUTING_KEY);
    }

    @Bean("confirmQueue")
    public Queue confirmQueue(){
        return QueueBuilder.durable(Constants.CONFIRM_QUEUE).build();
    }

    @Bean("confirmExchange")
    public DirectExchange confirmExchange(){
        return ExchangeBuilder.directExchange(Constants.CONFIRM_EXCHANGE).build();
    }

    @Bean("confirmQueueBinding")
    public Binding confirmBinding(@Qualifier("confirmExchange") DirectExchange exchange,
                                  @Qualifier("confirmQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("confirm");
    }

    @Bean("confirmRabbitTemplate")
    public RabbitTemplate confirmRabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                if (ack){
                    System.out.println("成功接受到消息：" + correlationData.getId());
                }else {
                    System.out.println("接收消息失败：" + correlationData.getId() + " " + cause);
                }
            }
        });
        return rabbitTemplate;
    }

    @Bean("rabbitTemplate")
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
        return new RabbitTemplate(connectionFactory);
    }

    @Bean("ttlQueue1")
    public Queue ttlQueue1(){
        return QueueBuilder.durable(Constants.TTL_QUEUE1).build();
    }

    @Bean("ttlQueue2")
    public Queue ttlQueue2(){
        return QueueBuilder.durable(Constants.TTL_QUEUE2).ttl(20000).build();
    }

    @Bean("ttlExchange")
    public DirectExchange ttlExchange(){
        return ExchangeBuilder.directExchange(Constants.TTL_EXCHANGE).build();
    }

    @Bean("ttlQueueBinding1")
    public Binding ttlQueueBinding1(@Qualifier("ttlExchange") DirectExchange exchange,
                                  @Qualifier("ttlQueue1") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(Constants.TTL_ROUTING_KEY);
    }

    @Bean("ttlQueueBinding2")
    public Binding ttlQueueBinding2(@Qualifier("ttlExchange") DirectExchange exchange,
                                  @Qualifier("ttlQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(Constants.TTL_ROUTING_KEY);
    }

}
